<?php
/**
 * 使用PHP源码实现的视图方案
 * 
 * 示例action：
 * <code>
 * 	public function appIndex() {
 * 		$this->developers = array("iwind", "danny", "peter");
 * 		return new IModelAndPHP($this, "tpl.php");
 * 	}
 * 	</code>
 * 
 * <i>tpl.php</i>
 * <code>
 * <ul>
 *<?php
 *
 *foreach ($developers as $developer) {
 *	echo "<li>" . $developer . "</li>";
 *}
 *
 *?>
 *</ul>
 * </code>
 * 
 * 不是以字母或下划线开头的，使用var_前缀。
 *
 * @version $Id: IModelAndPHP.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mvc
 * @since 0.1
 */
class IModelAndPHP extends IAbstractViewScheme {
	private $model;
	private $source;
	private $values = array();
	
	/**
	 * 构造
	 *
	 * @param mixed $model 视图方案，为数组或者对象
	 * @param string $sourceFile PHP源码文件
	 * @since 0.1
	 */
	public function __construct($model, $sourceFile) {
		$this->model = $model;
		$this->source = $sourceFile;		
	}
	
	/**
	 * 执行
	 *
	 * @since 0.1
	 */
	public function invoke() {
		if (is_object($this->model)) {
			$vars = get_object_vars($this->model);
		}
		else if (is_array($this->model)) {
			$vars = $this->model;
		}
		extract($this->values, EXTR_PREFIX_INVALID, "var");
		extract($vars, EXTR_PREFIX_INVALID, "var");
		if (is_file($this->source)) {
			require_once($this->source);
		}
		else {
			throw new IException("PHP source file '{$this->source}' does not exist");
		}
	}
	
	/**
	 * 添加键值对
	 *
	 * @param string $key 键
	 * @param mixed $value 值
	 */
	public function put($key, $value) {
		$this->values[$key] = $value;
	}
}


?>